#ifndef __MYTHREAD_HPP__
#define __MYTHREAD_HPP__

#include <iostream>
#include <pthread.h>
#include <functional>

namespace Mythread
{
	using func_t = std::function<void(std::string)>;
	

	class Thread
	{
	public:
		void Excute()
		{
			_func(_threadname);
		}
	public:
		Thread(func_t func,const std::string &name = "none-name")
		:_func(func),_threadname(name),_stop(true)
		{}
		
		static void* threadroutine(void* args)		 // 类成员函数，形参是有this指针的！
		{
			Thread* self = static_cast<Thread*>(args);
			self->Excute();
			return nullptr;
		}

		bool start()
		{
			int n = pthread_create(&_tid,nullptr,threadroutine,this);
			if(!n)
			{
				_stop = false;
				return true;
			}
			else
			{
				return false;
			}
		}

		bool join()
		{
			if(!_stop)
			{
				pthread_join(_tid,nullptr);
			}

			return true;
		}

		void detach()
		{
			if(!_stop)
			{
				pthread_detach(_tid);
			}
		}

		std::string name()
		{
			return _threadname;
		}

		void stop()
		{
			_stop = true;
		}

		~Thread(){}
	private:
		pthread_t _tid;				//线程id
		std::string _threadname;	//线程名
		//T &_data;					//数据
		func_t _func;			//执行任务
		bool _stop;					//启动状态
	};
}

#endif